package com.xunqi.scheduled;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.xunqi.dao.master.IStatFeedbackDao;
import com.xunqi.dao.master.IStatPushDao;
import com.xunqi.util.DateUtil;

/**
 * 插件定时任务
 * 
 * @author yemei
 *
 */
@Component
public class PluginTask {
	private static final Logger logger = LoggerFactory.getLogger(PluginTask.class);

	@Autowired
	private IStatFeedbackDao statFeedbackDao;

	@Autowired
	private IStatPushDao statPushDao;

	// @Scheduled(initialDelay = 10000, fixedDelay = 10000)
	public void initDelay() {
		logger.info("initialDelay task:[{}]", System.currentTimeMillis());
		return;
	}

	// @Scheduled(cron = "${task.plugin.feedback}")
	public void fixedDelay() {
		logger.info("fixedDelay task:[{}]", System.currentTimeMillis());
		return;
	}

	/**
	 * 反馈每日任务
	 */
	@Scheduled(cron = "${task.ps.stat.feedback.daily}")
	public void statFeedbackDaily() {
		for (int beginNum = 3; beginNum > 0; beginNum--) {
			// 1、判断当前日期数据是否汇总
			String maxAccessDate = statFeedbackDao.maxFeedbackDailyAccessdate();
			String genBeginDate = DateUtil.genBeginDate(beginNum);
			String genBeginDateThen = DateUtil.genBeginDateThen(beginNum);
			if (genBeginDate.compareTo(maxAccessDate) <= 0) {
				continue;
			}
			// 2、汇总
			int resultStat = statFeedbackDao.saveStatFeedbackDaily(genBeginDate, genBeginDateThen);
			// 3、更新apkname
			int resultUpdate = statFeedbackDao.updateFeedbackDailyApkname(genBeginDate);
			logger.info("statFeedbackDaily:beginDate:[{}],endDate[{}],resultStat[{}],resultUpdate[{}]", genBeginDate,
					genBeginDateThen, resultStat, resultUpdate);
		}
		return;
	}

	/**
	 * 推送每日任务
	 */
	@Scheduled(cron = "${task.ps.stat.push.daily}")
	public void statPushDaily() {
		for (int beginNum = 3; beginNum > 0; beginNum--) {
			// 1、判断当前日期数据是否汇总
			String maxAccessDate = statPushDao.maxPushDailyAccessdate();
			String genBeginDate = DateUtil.genBeginDate(beginNum);
			String genBeginDateThen = DateUtil.genBeginDateThen(beginNum);
			if (genBeginDate.compareTo(maxAccessDate) <= 0) {
				continue;
			}
			// 2、汇总
			int resultStat = statPushDao.saveStatPushDaily(genBeginDate, genBeginDateThen);
			// 3、更新apkname
			int resultUpdate = statPushDao.updatePushDailyApkname(genBeginDate);
			logger.info("statPushDaily:beginDate:[{}],endDate[{}],resultStat[{}],resultUpdate[{}]", genBeginDate,
					genBeginDateThen, resultStat, resultUpdate);
		}
		return;
	}

}
